package org.phantom.securityframework.datapermission.internal;

import java.lang.annotation.Annotation;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.phantom.securityframework.api.exception.AnnotationConfigureException;
import org.phantom.securityframework.datapermission.DataFilter;
import org.phantom.securityframework.datapermission.DimensionQuery;
import org.springframework.stereotype.Service;


/**
 * 执行自定义sql的DataProvider
 * 当遇到复杂过滤SimpleSqlDataProvider不能满足时可以使用
 * 不许要扩展框架,只需要在mapper文件中编写自定义查询语句,然后配置mapperId即可工作
 * @author apple
 *
 */
@Service("mapperDataProvider")
public class MapperDataProvider extends AbstractDataProvider{

	@Resource
	private SqlSession session = null;
	@Override
	public Object provide(Object[] args, Annotation anno)throws AnnotationConfigureException  {
		DataFilter df = (DataFilter) anno;
		DimensionQuery query = this.createDimensionQuery(args, anno);
		return session.selectList(df.mapper()[0],query);
	}

	@Override
	public int total(Object[] args, Annotation anno) {
		DataFilter df = (DataFilter) anno;
		return (Integer) session.selectOne(df.mapper()[1],this.createDimensionQuery(args,anno));
	}
}
